import 'package:cloud_medicine/components/basic_info.dart';
import 'package:cloud_medicine/model/base_user_info.dart';
import 'package:cloud_medicine/static/my_function.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MyPage extends StatefulWidget {
  @override
  _MyPageState createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  BaseUserInfo _user;

  @override
  void initState() {
    super.initState();
    _user = user;
  }

  // 判断用户身份是否是药店
  isShop() {
    if (user != null && user.identify.id == 5) {
      return true;
    }
    return false;
  }

  // 获取个人中心功能列表
  getMyFunctionList() {
    List<MyFunctionItem> _list = [];
    if (isShop()) {
      _list.add(MyFunctionItem.init({
        'icon': Icons.shop,
        'text': '药店管理',
      }));
    }
    _list.addAll(myFunctionStatic);
    if (_user != null) {
      _list.insert(_list.length - 2,
          MyFunctionItem.init({'icon': Icons.vpn_key, 'text': '修改密码'}));
    }
    return _list;
  }

  // 进入不同的页面
  enterVarious(String type) async {
    if (type == '退出登录') {
      user = null;

      // 清除token
      SharedPreferences prefs = await SharedPreferences.getInstance();
      prefs.remove('Authorization');

      Fluttertoast.showToast(
        msg: '成功退出',
        backgroundColor: Colors.green,
        textColor: Colors.white,
      );
      setState(() {
        _user = null;
      });
      return;
    }
    Map<String, String> contrast = {
      '订单中心': '/orderList',
      '消息中心': '/message',
      '关于我们': '/about',
      '错误中心': '/error',
      '收藏药店': '/collectShop',
      '收货地址': '/address',
      '修改密码': '/alter',
      '药店管理': '/shopManage'
    };
    Navigator.pushNamed(context, contrast[type]);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[200],
      appBar: AppBar(
        title: Text('个人中心'),
        centerTitle: true,
        automaticallyImplyLeading: false,
        actions: <Widget>[
          _user != null && _user.identify.id == 6
              ? Tooltip(
                  child: IconButton(
                    icon: Icon(Icons.home),
                    onPressed: () {
                      Navigator.pushNamed(context, '/back');
                    },
                  ),
                  message: '进入后台',
                )
              : Container(),
        ],
      ),
      body: ListView(
        children: <Widget>[
          MyBasicInfo(
            user: _user,
          ),
          ListView.builder(
            shrinkWrap: true,
            physics: new NeverScrollableScrollPhysics(),
            itemCount: getMyFunctionList().length,
            itemBuilder: (BuildContext context, int index) {
              if (_user == null && index == getMyFunctionList().length - 1) {
                return Container();
              }
              return Material(
                color: Colors.white,
                child: InkWell(
                  splashColor: Colors.grey.withOpacity(0.3), // 慢慢展开的颜色
                  highlightColor: Colors.grey.withOpacity(0.1), // 高亮颜色
                  child: Column(
                    children: <Widget>[
                      index > (getMyFunctionList().length - 3)
                          ? Container(height: 15, color: Colors.grey[200])
                          : Container(),
                      Container(
                          padding: EdgeInsets.only(
                            left: 10,
                            right: 10,
                            top: 20,
                            bottom: 20,
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Row(
                                children: <Widget>[
                                  Icon(
                                    getMyFunctionList()[index].icon,
                                    color: Colors.lightBlueAccent,
                                    size: 26,
                                  ),
                                  SizedBox(width: 10),
                                  Text(
                                    getMyFunctionList()[index].text,
                                    style: TextStyle(fontSize: 20),
                                  ),
                                ],
                              ),
                              Icon(
                                Icons.arrow_forward_ios,
                                size: 12,
                              )
                            ],
                          ))
                    ],
                  ),
                  onTap: () {
                    if (_user == null) {
                      Fluttertoast.showToast(
                        msg: '请先登录',
                        backgroundColor: Colors.black,
                        textColor: Colors.white,
                      );
                      return;
                    }
                    enterVarious(getMyFunctionList()[index].text);
                  },
                ),
              );
            },
          )
        ],
      ),
    );
  }
}
